package com.ten.mall.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ten.mall.service.FenRuleService;
import com.ten.mall.service.JifenService;
import com.ten.mall.vo.FenRule;
import com.ten.mall.vo.Jifen;
import com.ten.mall.vo.User;

@Controller
public class FenRuleController {
	@Autowired
	private FenRuleService fenRuleService;
	@Autowired
	private JifenService jifenService;
	@RequestMapping("/admin/jifenList")
	public String fenList(@RequestParam(name="start",defaultValue="1") Integer start,
						  @RequestParam(name="size",defaultValue="100") Integer size,
			              HttpServletRequest res){
		List<FenRule> list =fenRuleService.selectByPages(start, size, null);
		res.setAttribute("list", list);
		return "jsp/jifenList";
	}
	@RequestMapping("/admin/toJifen")
	public String toJifen(HttpServletRequest res,Integer id){
		if(id!=null){
			FenRule f =fenRuleService.selectById(id);
			res.setAttribute("f", f);
		}
		return "jsp/jifenAdd";
	}
	
	@RequestMapping("/admin/saveJifen")
	@ResponseBody
	public String addUser(FenRule f){
		String rs ="error";
		try {
			if(f.getId()==null){
				fenRuleService.insertPrivilege(f);
			}else{
				fenRuleService.updatePrivilege(f);
			}
			rs ="ok";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rs;
	}
	@RequestMapping("/admin/delJifen")
	@ResponseBody
	public String fenDel(Integer id){
		String rs="ok";
		try {
			fenRuleService.deletePrivilege(id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			rs="error";
		}
		return rs;
	}
	
	@RequestMapping("/phone/jifen")
	public String myJifen(HttpServletRequest res){
		User u =(User) res.getSession().getAttribute("u");
		if(u!=null){
			Jifen jifen=jifenService.selectByUserId(u.getId());
			res.setAttribute("jifen", jifen);
		}
		return "phone/jifen";
	}
}
